package com.hsnn.medstgmini.base.std.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.base.std.model.StdCompinfoLog;
import com.hsnn.medstgmini.base.std.dao.StdCompinfoLogDao;
import com.hsnn.medstgmini.util.Pagination;

/**企业库维护记录实现类 SERVICE
 * 
 * @author hss@njhsnn
 * 2015年6月9日
 */
@Repository
public class StdCompinfoLogDaoImpl extends BaseHibernateDAO implements StdCompinfoLogDao {


	/**
	 *  企业库 维护记录添加
	 * @param stdCompinfoLog
	 */
	@Transactional
	@Override
	public void add(StdCompinfoLog stdCompinfoLog) {
		this.getSession().saveOrUpdate(stdCompinfoLog);
		getSession().flush();
	}

	/**
	 * 企业库 维护记录查询所有及条件查询
	 * @param stdCompinfoLog
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>> queryStdCompinfoLogByName(
			Pagination page,StdCompinfoLog stdCompinfoLog) {
		String compId = stdCompinfoLog.getCompId();
		String date1 = stdCompinfoLog.getDate1();
		String date2 = stdCompinfoLog.getDate2();
		Short status = stdCompinfoLog.getStatus();
		StringBuffer hql = new StringBuffer();
		StringBuffer countHql = new StringBuffer();
		hql.append(" select scl.detail,");
		hql.append(" scl.comp_id compId,scl.comp_code compCode,scl.comp_name compName,scl.comp_type compType, ");
		hql.append(" scl.org_code orgCode,dica.ar_fullname arfullname,sct.comp_reg_name compRegName, ");
		hql.append(" scl.status,s.name updUser,DATE_FORMAT(scl.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetimeStr,scg.comp_group_name compGroupName  ");
		hql.append(" from std_compinfo_log scl ");
		hql.append(" LEFT JOIN sys_user s ON s.id=scl.upd_user ");
		hql.append(" left join std_compinfo_detail_log scdl on scl.log_id = scdl.log_id ");
		hql.append(" left join dic_area dica on scl.area_id = dica.area_id ");
		hql.append(" left join std_compgroup scg on scl.comp_group_id = scg.comp_group_id ");
		hql.append(" left join dic_comptype sct on scl.comp_reg_id = sct.comp_reg_id ");
		hql.append(" where ");
		hql.append(" scl.comp_id = :compId ");
		
		
		countHql.append(" select count(*) ");	
		countHql.append(" from std_compinfo_log scl ");
		countHql.append(" left join std_compinfo_detail_log scdl on scl.log_id = scdl.log_id ");
		countHql.append(" left join dic_area dica on scl.area_id = dica.area_id ");
		countHql.append(" left join std_compgroup scg on scl.comp_group_id = scg.comp_group_id ");
		countHql.append(" left join dic_comptype sct on scl.comp_reg_id = sct.comp_reg_id ");
		countHql.append(" where ");
		countHql.append(" scl.comp_id = :compId ");
		if(StringUtils.isNotBlank(date1)){
		hql.append(" and scl.upd_datetime >= :date1 ");
		countHql.append(" and scl.upd_datetime >= :date1 ");
		}
		if(StringUtils.isNotBlank(date2)){
			hql.append(" and scl.upd_datetime <= :date2  ");
			countHql.append(" and scl.upd_datetime <= :date2  ");
		}
		if(null != status ){
			hql.append(" and scl.status = :status  ");
			countHql.append(" and scl.status = :status  ");
		}
		hql.append(" order by scl.upd_datetime desc  ");
		Query countQuery = getSession().createSQLQuery(countHql.toString());
		Query query = getSession().createSQLQuery(hql.toString());
		query.setString("compId", compId);
		countQuery.setString("compId", compId);
		if(StringUtils.isNotBlank(date1)){
				query.setString("date1", date1);
				countQuery.setString("date1", date1);
			}
		if(StringUtils.isNotBlank(date2)){
				query.setString("date2", date2);
				countQuery.setString("date2", date2);
			}
		if(null != status ){
			query.setShort("status", status);
			countQuery.setShort("status", status);
		}
		stdCompinfoLog.setCount(Long.valueOf(countQuery.uniqueResult().toString()));
		query.setFirstResult((page.getPage()-1)*page.getMaxResults());
		query.setMaxResults(page.getMaxResults());
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return query.list();
	}
}
